package com.cskaoyan.service;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.cskaoyan.bean.user.User;
import com.cskaoyan.bean.wxsearch.RegisterBO;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.util.Md5HashUtil;
import com.cskaoyan.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;


@Service
public class WXAuthServiceImpl implements WXAuthService {

    @Autowired
    UserMapper userMapperl;

    String uuid6;

    @Override
    public String authRegCaptcha(String mobile) {

        DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", "LTAI5t8gpxPTCR6W58RnZq4u", "mVPbpbxia0JQotb7HyJAREV8QUuq8h");

        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");


        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", "stone4j");
        request.putQueryParameter("TemplateCode", "SMS_173765187");

        String uuid7 = UUIDUtil.getUUID6();
        uuid6 = uuid7;
        request.putQueryParameter("TemplateParam", "{\"code\": \"" + uuid7 + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
//            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return uuid6;
    }

    @Override
    public void authRegister(RegisterBO registerBO) {


        User user = new User();
        user.setUsername(registerBO.getUsername());
        String password = registerBO.getPassword();
        String Md5PassWord = Md5HashUtil.Md5encryption(password);
        user.setPassword(Md5PassWord);
        user.setWeixinOpenid(registerBO.getWxCode());
        user.setMobile(registerBO.getMobile());
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        user.setAddTime(date);
        userMapperl.insertSelective(user);


    }

    @Override
    public int authReset(Map map) {


        String mobile = (String) map.get("mobile");
        String password = (String) map.get("password");
        String Md5PassWord = Md5HashUtil.Md5encryption(password);
        userMapperl.password(mobile, Md5PassWord);
        return 200;


    }


}
